package com.cloud.alibaba.common.util;

import java.math.BigDecimal;
import java.math.RoundingMode;

public class computeUtil {

    public static double sum(double d1, double d2) {
        BigDecimal bd1 = new BigDecimal(Double.toString(d1));
        BigDecimal bd2 = new BigDecimal(Double.toString(d2));
        return bd1.add(bd2).doubleValue();
    }

    public static double sub(double d1, double d2) {
        BigDecimal bd1 = new BigDecimal(Double.toString(d1));
        BigDecimal bd2 = new BigDecimal(Double.toString(d2));
        return bd1.subtract(bd2).doubleValue();
    }

    public static double mul(double d1, double d2) {
        BigDecimal bd1 = new BigDecimal(Double.toString(d1));
        BigDecimal bd2 = new BigDecimal(Double.toString(d2));
        return bd1.multiply(bd2).doubleValue();
    }

    public static double div(double d1, double d2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException();
        } else {
            BigDecimal bd1 = new BigDecimal(Double.toString(d1));
            BigDecimal bd2 = new BigDecimal(Double.toString(d2));
            return bd1.divide(bd2, scale, RoundingMode.CEILING).doubleValue();
        }
    }

    public static double div(double d1, double d2, int scale,RoundingMode roundingMode) {
        if (scale < 0) {
            throw new IllegalArgumentException();
        } else {
            BigDecimal bd1 = new BigDecimal(Double.toString(d1));
            BigDecimal bd2 = new BigDecimal(Double.toString(d2));
            return bd1.divide(bd2, scale, roundingMode).doubleValue();
        }
    }
    public static double round(double d1, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException();
        } else {
            BigDecimal bd1 = new BigDecimal(Double.toString(d1));
            BigDecimal one = new BigDecimal("1");
            return bd1.divide(one, scale, RoundingMode.CEILING).doubleValue();
        }
    }

    public static boolean equals(Double d1, Double d2) {
        if (d1 == null && d2 == null) {
            return true;
        }
        if (d1 == null || d2 == null) {
            return false;
        }
        return d1.doubleValue() == d2.doubleValue();
    }
}
